package com.edu.ucenterservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.commonutils.JwtUtils;
import com.edu.commonutils.MD5;
import com.edu.commonutils.Results;
import com.edu.servicebase.exceptionhandler.MyException;
import com.edu.ucenterservice.domain.UcenterMember;
import com.edu.ucenterservice.domain.vo.LoginVo;
import com.edu.ucenterservice.domain.vo.RegisterVo;
import com.edu.ucenterservice.mapper.UcenterMemberMapper;
import com.edu.ucenterservice.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author ljw
 * @since 2022-09-22
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//手机号
        String nickname = registerVo.getNickname();//昵称
        String password = registerVo.getPassword();//密码


        //不能为空否则注册  非空判断
        if(StringUtils.isEmpty(code)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(password)){
            try {
                throw  new MyException(Results.ERROR,"注册失败");
            } catch (MyException e) {
                throw new RuntimeException(e);
            }
        }

        //获取验证码
        String rediscode = redisTemplate.opsForValue().get(mobile);
            System.out.println(rediscode+"*********************************************"+code);

        //比较验证码
        if(!code.equals(rediscode)){
            try {
                throw  new MyException(Results.ERROR,"验证码失效");
            } catch (MyException e) {
                throw new RuntimeException(e);
            }
        }
        //手机号判断
        //不添加相同手机号
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);

        if(count>0){
            try {
                throw  new MyException(Results.ERROR,"手机号已存在");
            } catch (MyException e) {
                throw new RuntimeException(e);
            }
        }
        UcenterMember ucenterMember=new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(MD5.encrypt(password));//密码加密
        ucenterMember.setNickname(nickname);
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("guangzhou.aliyuncs.com/2022/08/05/230e195737f24c04a7aad437bbe8864bfile.png");

        baseMapper.insert(ucenterMember);

    }

    @Override
    public String login(LoginVo loginVo) {
        //获取登录的手机号和密码
        String number=loginVo.getMobile();
        String password=loginVo.getPassword();

        //不能为空否则登陆失败
        if(StringUtils.isEmpty(number)||StringUtils.isEmpty(number)){
            try {
                throw  new MyException(Results.ERROR,"登录失败");
            } catch (MyException e) {
                throw new RuntimeException(e);
            }
        }
        //手机号是否正确
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper();
        wrapper.eq("mobile",number);
        UcenterMember ucenterMember = baseMapper.selectOne(wrapper);


        //账号判断是否为空 //没有这个手机号
        if(ucenterMember==null){
            try {
                throw  new MyException(Results.ERROR,"没有这个手机号");
            } catch (MyException e) {
                throw new RuntimeException(e);
            }
        }
        //判断密码  因为密码是加密的所以得先加密再比较
        if(!MD5.encrypt(password).equals(ucenterMember.getPassword())){
            try {
                throw  new MyException(Results.ERROR,"密码错误");
            } catch (MyException e) {
                throw new RuntimeException(e);
            }
        }

        if(ucenterMember.getIsDisabled()){
            try {
                throw  new MyException(Results.ERROR,"该账号被禁用");
            } catch (MyException e) {
                throw new RuntimeException(e);
            }
        }
            //生成token值
        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());
        return token;
    }

    @Override
    public UcenterMember getByOpenid(String openid) {

        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);

        UcenterMember member = baseMapper.selectOne(queryWrapper);
        return member;
    }

    @Override
    public int countDate(String day) {
        return baseMapper.countByDate(day);
    }
}
